<
<template>
  <div id="add-blog">
    <h2>添加博客</h2>
    <form v-if="!submmited">
      <label>博客标题</label>
      <input type="text" v-model="blog.title" required />
      <label>博客内容</label>
      <textarea v-model="blog.content"></textarea>
      <div id="checkboxes">
        <label>小学课程</label>
        <input type="checkbox" value="小学课程" v-model="blog.categories" />
        <label>初中课程</label>
        <input type="checkbox" value="初中课程" v-model="blog.categories" />
        <label>高中课程</label>
        <input type="checkbox" value="高中课程" v-model="blog.categories" />
        <label>大学课程</label>
        <input type="checkbox" value="大学课程" v-model="blog.categories" />
      </div>
      <label>作者:</label>
      <select v-model="blog.author">
        <option v-for="author in authors">
          {{ author }}
        </option>
      </select>
      <button @click.prevent="post">添加博客</button>
    </form>
    <div>
      <h3 v-if="submmited">您的博客发布成功！</h3>
    </div>
    <div id="preview">
      <h3>博客总览</h3>
      <p>博客标题:{{ blog.title }}</p>
      <p>博客内容:</p>
      <p>{{ blog.content }}</p>
      <p>博客分类</p>
      <ul>
        <li v-for="category in blog.categories">
          {{ category }}
        </li>
      </ul>
      <p>作者:{{ blog.author }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'add-blog',
  data() {
    return {
      blog: {
        title: '',
        content: '',
        categories: [],
        author: '',
      },
      authors: ['DCrui', '杨老师', '赵老师'],
      submmited: false, //表单默认不展示
    };
  },
  methods: {
    post() {
      //https://jsonplaceholder.typicode.com/posts
      this.$http
        .post(
          'https://vuedemo-5710a-default-rtdb.firebaseio.com/posts.json',
          this.blog
          // {
          //   title: this.blog.title,
          //   body: this.blog.content,
          //   userId: 1,
          // }
        )
        .then(function (data) {
          //post成功，返回数据
          console.log(data);
          this.submmited = true; //点击添加博客，隐藏表单
        });
    },
  },
};
</script>

<style lang="less" scoped>
#add-blog * {
  //所有样式都是box-sizing
  box-sizing: border-box;
}
#add-blog {
  margin: 20px auto;
  max-width: 600px;
  padding: 20px;
}
label {
  display: block; //每个label独占一行
  margin: 20px 0 10px;
}
//input,textarea,select共同样式
input[type='text'],
textarea,
select {
  display: block;
  width: 100%;
  padding: 8px;
}
//博客内容
textarea {
  height: 200px;
}
#checkboxes label {
  //多选占一行
  display: inline-block;
  margin-top: 0px;
}
#checkboxes input {
  display: inline-block; //多选框向右拉开距离
  margin-right: 10px;
}
//按钮样式
button {
  display: block;
  margin: 20px 0;
  background: crimson; //红色
  //11.16修改
  color: #fff;
  border: 0px;
  padding: 14px;
  border-radius: 4px; //圆角
  font-size: 18px;
  cursor: pointer; //进入变小手状态
}
//博客总览样式
#preview {
  padding: 10px 20px;
  border: 1px dotted #ccc; //虚线
  margin: 30px 0;
  h3 {
    margin-top: 10px;
  }
}
</style>
